// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DataGridViewDisableButtonCell.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.UI.Forms
{
    #region Usings

    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.VisualStyles;

    #endregion

    /// <summary>
    /// The data grid view disable button cell.
    /// </summary>
    public class DataGridViewDisableButtonCell : DataGridViewButtonCell
    {
        #region Constants and Fields

        /// <summary>
        /// The enabled value.
        /// </summary>
        private bool enabledValue;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DataGridViewDisableButtonCell"/> class.
        /// </summary>
        public DataGridViewDisableButtonCell()
        {
            this.enabledValue = true;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets a value indicating whether Enabled.
        /// </summary>
        public bool Enabled
        {
            get
            {
                return this.enabledValue;
            }

            set
            {
                this.enabledValue = value;
            }
        }

        #endregion

        // Override the Clone method so that the Enabled property is copied.
        #region Public Methods

        /// <summary>
        /// The clone.
        /// </summary>
        /// <returns>
        /// The clone.
        /// </returns>
        public override object Clone()
        {
            var cell = (DataGridViewDisableButtonCell)base.Clone();
            cell.Enabled = this.Enabled;
            return cell;
        }

        #endregion

        // By default, enable the button cell.
        #region Methods

        /// <summary>
        /// The paint.
        /// </summary>
        /// <param name="graphics">
        /// The graphics.
        /// </param>
        /// <param name="clipBounds">
        /// The clip bounds.
        /// </param>
        /// <param name="cellBounds">
        /// The cell bounds.
        /// </param>
        /// <param name="rowIndex">
        /// The row index.
        /// </param>
        /// <param name="elementState">
        /// The element state.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <param name="formattedValue">
        /// The formatted value.
        /// </param>
        /// <param name="errorText">
        /// The error text.
        /// </param>
        /// <param name="cellStyle">
        /// The cell style.
        /// </param>
        /// <param name="advancedBorderStyle">
        /// The advanced border style.
        /// </param>
        /// <param name="paintParts">
        /// The paint parts.
        /// </param>
        protected override void Paint(
            Graphics graphics, 
            Rectangle clipBounds, 
            Rectangle cellBounds, 
            int rowIndex, 
            DataGridViewElementStates elementState, 
            object value, 
            object formattedValue, 
            string errorText, 
            DataGridViewCellStyle cellStyle, 
            DataGridViewAdvancedBorderStyle advancedBorderStyle, 
            DataGridViewPaintParts paintParts)
        {
            // The button cell is disabled, so paint the border,  
            // background, and disabled button for the cell.
            if (!this.enabledValue)
            {
                // Draw the cell background, if specified.
                if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
                {
                    var cellBackground = new SolidBrush(cellStyle.BackColor);
                    graphics.FillRectangle(cellBackground, cellBounds);
                    cellBackground.Dispose();
                }

                // Draw the cell borders, if specified.
                if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
                {
                    this.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
                }

                // Calculate the area in which to draw the button.
                var buttonArea = cellBounds;
                var buttonAdjustment = this.BorderWidths(advancedBorderStyle);
                buttonArea.X += buttonAdjustment.X;
                buttonArea.Y += buttonAdjustment.Y;
                buttonArea.Height -= buttonAdjustment.Height;
                buttonArea.Width -= buttonAdjustment.Width;

                // Draw the disabled button.                
                ButtonRenderer.DrawButton(graphics, buttonArea, PushButtonState.Disabled);

                // Draw the disabled button text. 
                if (this.FormattedValue is string)
                {
                    TextRenderer.DrawText(
                        graphics, (string)this.FormattedValue, this.DataGridView.Font, buttonArea, SystemColors.GrayText);
                }
            }
            else
            {
                // The button cell is enabled, so let the base class 
                // handle the painting.
                base.Paint(
                    graphics, 
                    clipBounds, 
                    cellBounds, 
                    rowIndex, 
                    elementState, 
                    value, 
                    formattedValue, 
                    errorText, 
                    cellStyle, 
                    advancedBorderStyle, 
                    paintParts);
            }
        }

        #endregion
    }
}